var express = require('express');
var router = express.Router();
const passport = require('passport');
var addressDAL = require('../DAL/addressDAL.js')

//修改收货地址
router.post('/changeAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.body
    addressDAL.changeAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '修改收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '修改收货地址成功',
                results: results
            })
        }
    })
})
//添加新收货地址
router.post('/addNewAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.body
    addressDAL.addNewAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '添加新收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '添加新收货地址成功',
                results: results
            })
        }
    })
})
//删除收货地址
router.post('/delAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.body
    addressDAL.delAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除收货地址成功',
                results: results
            })
        }
    })
})
//查看收货地址
router.get('/viewAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.query
    addressDAL.viewAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看收货地址成功',
                results: results
            })
        }
    })
})

router.get('/getUserSomeAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.query
    addressDAL.getUserSomeAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看某条收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看某条收货地址成功',
                results: results
            })
        }
    })
})
//设置默认地址
router.post('/setDefaultAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.body
    addressDAL.setDefaultAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '修改默认收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '修改默认收货地址成功',
                results: results
            })
        }
    })
})
router.post('/delDefaultAddress', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    var address = req.body
    addressDAL.delDefaultAddress(address, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除默认收货地址失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除默认收货地址成功',
                results: results
            })
        }
    })
})
module.exports = router;
